home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 101-125 / 118 / wiredemo / object.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  5KB  |  211 lines

  1.  
  2. /*
  3.  *  OBJECT.C
  4.  *
  5.  */
  6.  
  7. #include "bubbles.h"
  8.  
  9. extern long S_width, S_height, S_midwidth, S_dblheight;
  10.  
  11. static uword maxpts, maxcon;
  12.  
  13. static long X,Y,Z;
  14. static long CX,CY,CZ;
  15. static short Cost, Cosp, Sint, Sinp, CostCosp, CospSint, SintSinp, SinpCost;
  16. static long *Scrx, *Scry, *Scrz;
  17. static uword CTheta, CPhi;
  18.  
  19. OBJECT *
  20. makeobject(pts, npts, con, ncon, x, y, z)
  21. long *pts;
  22. uword *con;
  23. {
  24.     register OBJECT *obj = malloc(sizeof(OBJECT));
  25.     register short i;
  26.  
  27.     obj->points = pts;
  28.     obj->connect= con;
  29.     obj->npts = npts;
  30.     obj->ncon = ncon;
  31.     obj->x = x;
  32.     obj->y = y;
  33.     obj->z = z;
  34.     obj->theta = obj->phi = obj->rot = 0;
  35.  
  36.     if (npts > maxpts)    maxpts = npts;
  37.     if (ncon > maxcon)    maxcon = ncon;
  38.  
  39.     return(obj);
  40. }
  41.  
  42. init_objectmodule()
  43. {
  44.     Scrx = malloc(sizeof(long) * (maxpts+1));
  45.     Scry = malloc(sizeof(long) * (maxpts+1));
  46.     Scrz = malloc(sizeof(long) * (maxpts+1));
  47. }
  48.  
  49. setcamera(x,y,z,cx,cy,cz,rot)
  50. {
  51.     X=x;
  52.     Y=y;
  53.     Z=z;
  54.     CX=cx;
  55.     CY=cy;
  56.     CZ=cz;
  57.     CTheta = ibearing(cx-x,cy-y);
  58.     CPhi   = ibearing(irange(cx-x,cy-y),cz-z);
  59.  
  60.     Cost = ICOS(CTheta);    /*  returns signed -32767 to 32767  */
  61.     Cosp = ICOS(CPhi);
  62.     Sint = ISIN(CTheta);
  63.     Sinp = ISIN(CPhi);
  64.     CostCosp = (Cost * Cosp) >> 15;
  65.     CospSint = (Cosp * Sint) >> 15;
  66.     SintSinp = (Sint * Sinp) >> 15;
  67.     SinpCost = (Sinp * Cost) >> 15;
  68. }
  69.  
  70. setcamerad(x,y,z,cx,cy,cz,rot)
  71. {
  72.     setcamera(X+x,Y+y,Z+z,CX+cx,CY+cy,CZ+cz,rot);
  73. }
  74.  
  75.  
  76. displayobj(rp,obj)
  77. RP *rp;
  78. OBJECT *obj;
  79. {
  80.     register long x,y,z;
  81.     register long *scrx,*scry,*scrz;
  82.     short i;
  83.     long *p;
  84.     long dx,dy,dz;
  85.  
  86.     dx = obj->x - X;
  87.     dy = obj->y - Y;
  88.     dz = obj->z - Z;
  89.     scrx = Scrx;
  90.     scry = Scry;
  91.     scrz = Scrz;
  92.  
  93.     for (p = obj->points, i = obj->npts; i; --i, p += 3,++scrx,++scry,++scrz) {
  94.  
  95.     /*
  96.      *  Transform to center
  97.      */
  98.  
  99.     x = p[0]+dx;
  100.     y = p[1]+dy;
  101.     z = p[2]+dz;
  102.  
  103.     /*
  104.      *  Align with Z access pointing ahead (Z>0 is visible). X heading
  105.      *  to the right, Y heading up
  106.      */
  107.  
  108.     scrz[0] = (((short)x * CostCosp)>>15) + (((short)y * CospSint)>>15) + (((short)z * Sinp)>>15);
  109.     scrx[0] = -((((short)y * Cost)>>15) - (((short)x * Sint)>>15));
  110.     scry[0] = -(((short)x * SinpCost)>>15) - (((short)y * SintSinp)>>15) + (((short)z * Cosp)>>15);
  111.  
  112.     /*
  113.      *  Add Perspective approximation.
  114.      */
  115.  
  116.     if (scrz[0] > 0) {
  117.         scrx[0] = (scrx[0] << 8) / (scrz[0] + 256);
  118.         scry[0] = (scry[0] << 8) / (scrz[0] + 256);
  119.     } else {
  120.         scrx[0] = (scrx[0] * (-scrz[0] + 256)) >> 8;
  121.         scry[0] = (scry[0] * (-scrz[0] + 256)) >> 8;
  122.     }
  123.     }
  124.     connect(rp,obj->connect,obj->ncon);
  125. }
  126.  
  127. connect(rp,connect,ncon)
  128. register uword *connect;
  129. {
  130.     register long x1,y1,z1;
  131.     register long x2,y2,z2;
  132.     for (; ncon; --ncon, connect += 2) {
  133.     x1 = S_midwidth+Scrx[connect[0]];  x2 = S_midwidth+Scrx[connect[1]];
  134.     y1 = S_height-Scry[connect[0]]    ;  y2 = S_height-Scry[connect[1]];
  135.     z1 = Scrz[connect[0]];        z2 = Scrz[connect[1]];
  136.  
  137.     if (z1 < 0 && z2 < 0)
  138.         continue;
  139.     if (z1 < 0) {        /*    Calculate x & y intercept with window    */
  140.         x1 -= z1 * (x2 - x1) / (z2 - z1);
  141.         y1 -= z1 * (y2 - y1) / (z2 - z1);
  142.     }
  143.     if (z2 < 0) {
  144.         x2 -= z2 * (x1 - x2) / (z1 - z2);
  145.         y2 -= z2 * (y1 - y2) / (z1 - z2);
  146.     }
  147.  
  148.     if (y1 < 0) {        /*  CLIP PT A    */
  149.         if (y2 <= 0)
  150.         continue;
  151.         x1 += y1 * (x2 - x1) / (y1 - y2);
  152.         y1 = 0;
  153.     } else
  154.     if (y1 >= S_dblheight) {
  155.         if (y2 >= S_dblheight)
  156.         continue;
  157.         x1 += (y1 - S_dblheight) * (x2 - x1) / (y1 - y2);
  158.         y1 = S_dblheight-1;
  159.     }
  160.     if (x1 < 0) {
  161.         if (x2 <= 0)
  162.         continue;
  163.         y1 += x1 * (y2 - y1) / (x1 - x2);
  164.         x1 = 0;
  165.     } else
  166.     if (x1 >= S_width) {
  167.         if (x2 >= S_width)
  168.         continue;
  169.         y1 += (x1 - S_width) * (y2 - y1) / (x1 - x2);
  170.         x1 = S_width-1;
  171.     }
  172.     if (y2 < 0) {        /*  CLIP PT B    */
  173.         if (y1 <= 0)
  174.         continue;
  175.         x2 += y2 * (x1 - x2) / (y2 - y1);
  176.         y2 = 0;
  177.     } else
  178.     if (y2 >= S_dblheight) {
  179.         if (y1 >= S_dblheight)
  180.         continue;
  181.         x2 += (y2 - S_dblheight) * (x1 - x2) / (y2 - y1);
  182.         y2 = S_dblheight-1;
  183.     }
  184.     if (x2 < 0) {
  185.         if (x1 <= 0)
  186.         continue;
  187.         y2 += x2 * (y1 - y2) / (x2 - x1);
  188.         x2 = 0;
  189.     } else
  190.     if (x2 >= S_width) {
  191.         if (x1 >= S_width)
  192.         continue;
  193.         y2 += (x2 - S_width) * (y1 - y2) / (x2 - x1);
  194.         x2 = S_width-1;
  195.     }
  196.  
  197.     if (x1 < 0 || x1 >= S_width || y1 < 0 || y1 >= S_dblheight ||
  198.         x2 < 0 || x2 >= S_width || y1 < 0 || y2 >= S_dblheight) {
  199.         printf("Softerror: %ld %ld %ld %ld\n", x1, y1>>1, x2, y2>>1);
  200.     } else {
  201.         static long lastx,lasty;
  202.         if ((lastx != x1) || (lasty != y1))
  203.         Move(rp,x1,y1>>1);
  204.         Draw(rp,x2,y2>>1);
  205.         lastx = x2;
  206.         lasty = y2;
  207.     }
  208.     }
  209. }
  210.  
  211.